package com.wjm.androidnetproclient;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class URLConnectionActivity extends AppCompatActivity {

    private EditText et_spec;
    private Button btn_URLConnection;
    private TextView tv_result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_urlconnection);
        findViewById();
    }

    private void findViewById() {
        et_spec = findViewById(R.id.et_spec);
        btn_URLConnection = findViewById(R.id.btn_URLConnection);
        tv_result = findViewById(R.id.tv_result);
        btn_URLConnection.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                URLConnectionThread();
            }
        });
    }

    private void URLConnectionThread() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                URLConnectionTest();
            }
        }).start();
    }

    private void URLConnectionTest() {
        try {
            String spec = et_spec.getText().toString().trim();
            URL url = new URL(spec);
            URLConnection urlConnection = url.openConnection();
            urlConnection.connect();
            urlConnection.setConnectTimeout(5000);//5秒超时
            InputStream is = urlConnection.getInputStream();
            //获取头
            String headerField = urlConnection.getHeaderField("Content-Type");
            System.out.println(headerField);
            if ("text/html".equals(headerField)) {
                System.out.println("启动HTML解析器解析数据，并显示结果");
            } else {
                System.out.println("启动相应的解析器解析数据，并显示结果");
            }
            //读数据
            byte[] buf = new byte[1024];
            int len;
            StringBuilder sb = new StringBuilder();
            while ((len = is.read(buf)) != -1) {
                sb.append(new String(buf, 0, len));
            }
            is.close();
            //更新UI

            String sbStr = sb.toString();
            refreshUI(sbStr);

        } catch (MalformedURLException e) {
            e.printStackTrace();
            refreshUI(e.toString());
        } catch (IOException e) {
            e.printStackTrace();
            refreshUI(e.toString());
        }
    }

    private void refreshUI(final String result) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (!TextUtils.isEmpty(result)) {
                    tv_result.setText(result);
                }
            }
        });
    }
}
